//
//  YZMFundListModel.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/13.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMFundListModel.h"
#import "Mantle+Utils.h"

#define DEFAULT_FUND_LIST_RECHARGE      @"default_fund_list_recharge_"    /*充值*/
#define DEFAULT_FUND_LIST_WITHDRAW      @"default_fund_list_withdraw_"    /*提现*/
#define DEFAULT_FUND_LIST_INCOME        @"default_fund_list_income_"      /*收入*/
#define DEFAULT_FUND_LIST_OUTCOME       @"default_fund_list_outcome_"     /*支出*/

@implementation YZMFundListModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    
    return @{
             @"explain": @"explain",
             @"detail": @"detail",
             @"money": @"money",
             @"requestTime": @"requestTime",
             @"disposeTime": @"disposeTime",
             @"type": @"type",
             @"status": @"status",
             @"time": @"time",
             @"ID": @"id"};
}

+ (NSValueTransformer *)requestTimeJSONTransformer {

    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

+ (NSValueTransformer *)disposeTimeJSONTransformer {
    
    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

+ (NSValueTransformer *)timeJSONTransformer {
    
    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

- (void)setNilValueForKey:(NSString *)key{
    
}

- (NSString *)statusName {
    
    switch (self.status) {
        case 0:
            return @"未处理";
            break;
        case 1:
            return @"成功";
            break;
        case 2:
            return @"失败";
            break;
        case 3:
            return @"已取消";
            break;
        case 4:
            return @"处理中";
            break;
        case 5:
            return @"汇款中";
            break;
        default:
            break;
    }
    
    return @"";
}

- (UIColor *)statusColor {
    
    if (self.status == 1) {
        
        return [YZMAppStyle colorForIconColor];
    } else {
        
        return [YZMAppStyle colorForWhite];
    }
}

+ (void)createEncoder:(NSArray *)array forKey:(NSString *)keyName {
    
    if (!array || array.count == 0 || !keyName) {
        
        return;
    }
    
    NSMutableArray *archiverArray = [NSMutableArray array];
    
    for (YZMFundListModel *instance in array) {
        
        NSMutableData *data = [NSMutableData data];
        //创建一个归档类
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:instance forKey:keyName];
        [archiver finishEncoding];
        
        [archiverArray addObject:data];
    }
    
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:archiverArray forKey:keyName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSMutableArray *)createDecoder:(NSString *)keyName {
    
    if (!keyName) {
        
        return nil;
    }
    
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:keyName];
    
    if (!array ||array.count == 0) {
        
        return nil;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (NSData *data in array) {
        
        if (data && data.length != 0) {
            
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            
            YZMFundListModel *instance = [unarchiver decodeObjectForKey:keyName];
            [unarchiver finishDecoding];
            
            [tempArray addObject:instance];
        }
    }
    
    if (tempArray.count == 0) {
        
        return nil;
    }
    
    return tempArray;
}

+ (void)createEncoderNil {
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey: [DEFAULT_FUND_LIST_RECHARGE stringByAppendingString:YZMUserServiceInstance.currentUser.userID]];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey: [DEFAULT_FUND_LIST_INCOME stringByAppendingString:YZMUserServiceInstance.currentUser.userID]];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey: [DEFAULT_FUND_LIST_OUTCOME stringByAppendingString:YZMUserServiceInstance.currentUser.userID]];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey: [DEFAULT_FUND_LIST_WITHDRAW stringByAppendingString:YZMUserServiceInstance.currentUser.userID]];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
